package com.kd.service.jobSeeker.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kd.bean.jobSeeker.Basic_information;
import com.kd.bean.jobSeeker.Deliver_resume;
import com.kd.bean.jobSeeker.Doc_resumes;
import com.kd.bean.recruiter.Rec_news;
import com.kd.bean.recruiter.Recruitment_msg;
import com.kd.dao.jobSeeker.IBasic_informationDao;
import com.kd.dao.jobSeeker.IDeliver_resumeDao;
import com.kd.dao.jobSeeker.IDoc_resumesDao;
import com.kd.dao.recruiter.IRec_newsDao;
import com.kd.service.jobSeeker.IDeliver_resumeService;

@Service
@Transactional
public class Deliver_resumeServiceImpl implements IDeliver_resumeService{

	@Autowired
	private IDeliver_resumeDao delDao;
	
	@Autowired
	private IRec_newsDao recNewsDao;//招聘者相关的类 --招聘者 -- 求职者 发送信息（IRec_newsDao）--
	
	@Autowired
	private IBasic_informationDao basic_information;//求职者相关的类 --基本资料（Basic_information）--

	@Autowired
	private IDoc_resumesDao docDao;//求职者相关的类 --文档简历（Doc_resumes）--

	
	@Override
	public PageInfo<Deliver_resume> queryAllDeliverMessageByRecruiter_id(Integer pageNum, Integer pageSize,
			Integer recruiter_id) throws Exception {
		PageHelper.startPage(pageNum, pageSize);
		List<Deliver_resume> list = delDao.queryAllDeliverMessageByRecruiter_id(recruiter_id);
		PageInfo<Deliver_resume> info = new PageInfo<>(list);
		return info;
	}

	@Override
	public Boolean updateDeliveDr_state(Integer dr_id, Integer dr_state) throws Exception {
		Boolean flag = delDao.updateDeliveDr_state(dr_id, dr_state)>0;
		if(flag) {
			Deliver_resume delMessage = delDao.queryByRr_id(dr_id);//通过投递简历id 查询投递的简历
			//1、简历申请未通过
			if(dr_state==1) {
				Rec_news rec_news = new Rec_news();
				rec_news.setBasic_id(delMessage.getDoc_res().getBasic().getBasic_id());//求职者id'
				rec_news.setRec_news_context("面试未通过，希望您再接再厉！！！");//消息内容'
				rec_news.setRecruiter_id(delMessage.getRecruiter_id());//招聘者id'
				
				recNewsDao.insertRecNews(rec_news);
			}else if(dr_state==2){
				//2、简历申请通过
				Rec_news rec_news = new Rec_news();
				rec_news.setBasic_id(delMessage.getDoc_res().getBasic().getBasic_id());//求职者id'
				rec_news.setRec_news_context("面试通过，请您在一周内前来面试！！！");//消息内容'
				rec_news.setRecruiter_id(delMessage.getRecruiter_id());//招聘者id'
				
				recNewsDao.insertRecNews(rec_news);
			}
		}
		return flag;
	}

	@Override
	public Boolean updateDr_delete_state(Integer dr_id, Integer dr_delete_state) throws Exception {
		//修改求职者的面试申请状态 1、简历申请未通过 2、简历申请通过<发送面试邀请>
		return delDao.updateDr_delete_state(dr_id, dr_delete_state)>0;
	}

	@Override
	public Boolean insertDeliver_resume(Integer[] basic_id, Integer recruiter_id,
			Recruitment_msg recruitment_msg) throws Exception {
		
		List<Deliver_resume> deliver = new ArrayList<Deliver_resume>();
		
		for(Integer id:basic_id) {
			Deliver_resume de = new Deliver_resume();
			//通过应聘人id查询信息
			Basic_information getres_id = basic_information.queryByBasic_id(id);
			//查询文档信息通过求职者id
			List<Doc_resumes> listDocId = docDao.queryByBasic_id(getres_id.getBasic_id());
			if(listDocId!=null) {
				//拿到文档简历id<doc_id>
				de.setDoc_id(listDocId.get(0).getDoc_id());//'文档简历id',
				de.setRecruiter_id(recruiter_id);//'招聘人id',
				de.setRec_title(recruitment_msg.getRec_title());//招聘岗位标题
				de.setDr_state(2);//面试通过
				deliver.add(de);
				//-- 发送消息
				Rec_news rec_news = new Rec_news();
				rec_news.setBasic_id(id);//求职者id'
				rec_news.setRec_news_context("您的再线简历满足公司需求，请您在一周内前来面试，谢谢！！！");//消息内容'
				rec_news.setRecruiter_id(recruiter_id);//招聘者id'
				recNewsDao.insertRecNews(rec_news);
			}
		}
		
		return delDao.insertDeliver_resume(deliver)>0;
	}

	@Override
	public Boolean insertDeliver_resumeOne(Deliver_resume deliver_resume,Integer basic_id) throws Exception {
		
		//Integer count = basic_information.queryjudge_deliver_resumCounts(basic_id);
		//basic_information.updatejudge_deliver_resumCount((count+1), basic_id);
		return delDao.insertDeliver_resumeOne(deliver_resume)>0;
	}

	@Override
	public PageInfo<Deliver_resume> queryDeliver_resumeByDoc_id(Integer pageSize, Integer pageNum,Integer basic_id) throws Exception{
		Integer[] doc_id = docDao.queryAllDoc_idBybasic_id(basic_id);
		if(doc_id.length<=0) {
			List<Deliver_resume> list = new ArrayList<>();
			PageInfo<Deliver_resume> info = new PageInfo<>(list);
			return info;
		}
		PageHelper.startPage(pageNum, pageSize);
		List<Deliver_resume> list = delDao.queryDeliver_resumeByDoc_id(doc_id);
		PageInfo<Deliver_resume> info = new PageInfo<>(list);
		return info;
	}

	@Override
	public Boolean updateDr_download_state(Integer dr_download_state,Integer dr_id) throws Exception {
		
		return delDao.updateDr_download_state(dr_download_state, dr_id)>0;
	}

}
